#include "include/obd_function.h"
#include "include/obd_debug.h"

int selfTest(int fd, char *buf)
{
	int ret = 0;
	unsigned char wm[6]={0x00,0x00,0x00,0x00,0x00,0x00};
	unsigned char fc_data[5]={0x00,0x00,0x00,0x00,0x00};
    //Send Command
    memset(buf,0,rxbuf_size);

    /********** AT Commands *********/

    /*<----- General Commands ----->*/
    /*
    ret = AT_Repeat(fd,buf);
    printf("[AT_Repeat]----->[%d]:%s\n",ret,buf);

    ret = AT_Set_BRD(fd,buf,0x45);
    printf("[AT_BRD]----->[%d]:%s\n",ret,buf);

    ret = AT_Set_BRT(fd,buf,0x00);
    printf("[AT_BRT]----->[%d]:%s\n",ret,buf);


    ret = AT_Defaults(fd,buf);
    printf("[AT_Defaults]----->[%d]:%s\n",ret,buf);
    */

    ret = AT_Set_Echo(fd,buf,1);
    printf("[AT_Echo]----->[%d]:%s\n",ret,buf);

    ret = AT_Get_Version(fd,buf);
    printf("[AT_Version]----->[%d]:%s\n",ret,buf);

    ret = AT_Set_Linefeeds(fd,buf,0);
    printf("[AT_Linefeeds]----->[%d]:%s\n",ret,buf);

    /*
    ret = AT_EnterLowPower(fd,buf);
    printf("[AT_EnterLowPower]----->[%d]:%s\n",ret,buf);
    */

    ret = AT_Set_Memory(fd,buf,0x01);
    printf("[AT_Memory]----->[%d]:%s\n",ret,buf);

    //ret = AT_WarmStart(fd,buf);
    //printf("[AT_WarmStart]----->[%d]:%s\n",ret,buf);

    //ret = AT_Reset(fd,buf);
    //printf("[AT_Reset]----->[%d]:%s\n",ret,buf);

    ret = AT_Get_Description(fd,buf);
    printf("[AT_Description]----->[%d]:%s\n",ret,buf);

    ret = AT_Get_Identifier(fd,buf);
    printf("[AT_Identifier]----->[%d]:%s\n",ret,buf);

    //ret = AT_Set_Identifier(fd,buf,"Jason Liu");
    //printf("[AT_Set_Identifier]----->[%d]:%s\n",ret,buf);


    /*<----- Programmable Parameter Commands ----->*/
    ret = AT_Set_PP_OFF(fd,buf,0x00);
    printf("[AT_Set_PP_OFF]----->[%d]:%s\n",ret,buf);

    ret = AT_Set_PP_ON(fd,buf,0x00);
    printf("[AT_Set_PP_ON]----->[%d]:%s\n",ret,buf);

    ret = AT_PP_ON(fd,buf);
    printf("[AT_All_PP_ON]----->[%d]:%s\n",ret,buf);

    ret = AT_PP_OFF(fd,buf);
    printf("[AT_All_PP_OFF]----->[%d]:%s\n",ret,buf);

    ret = AT_Set_PP_VAL(fd,buf,0x00,0xFF);
    printf("[AT_PP_VAL]----->[%d]:%s\n",ret,buf);

//*
    ret = AT_Get_PPSummary(fd,buf);
    printf("[AT_Get_PP Summary]----->[%d]\n",ret);
    printf("%s\n",buf);
    //*/



    /*<----- Voltage Reading Commands ----->*/
    ret = AT_Set_CV(fd,buf,0000);
    printf("[AT_Set_Calibration]----->[%d]:%s\n",ret,buf);

    ret = AT_Get_Voltage(fd,buf);
    printf("[AT_Read_Voltage]----->[%d]:%s\n",ret,buf);


    /*<----- Other Commands ----->*/
    ret = AT_Get_Sleep(fd,buf);
    printf("[AT_Sleep_Level]----->[%d]:%s\n",ret,buf);


    /*<----- OBD Commands ----->*/
    ret = AT_AllowLong(fd,buf);
    printf("[AT_AllowLong]----->[%d]:%s\n",ret,buf);

    ret = AT_AutoReceive(fd,buf);
    printf("[AT_AutoReceive]----->[%d]:%s\n",ret,buf);

    ret = AT_Set_ADTiming(fd,buf,0x01);
    printf("[AT_AdaptiveTiming]----->[%d]:%s\n",ret,buf);

    ret = AT_BypassInit(fd,buf);
    printf("[AT_BypassInit]----->[%d]:%s\n",ret,buf);

    ret = AT_Get_Protocol(fd,buf);
    printf("[AT_Protocol]----->[%d]:%s\n",ret,buf);

    ret = AT_Get_ProtocolNumber(fd,buf);
    printf("[AT_ProtocolNumber]----->[%d]:%s\n",ret,buf);

    ret = AT_Set_Header(fd,buf,0x00);
    printf("[AT_SetHeader]----->[%d]:%s\n",ret,buf);

    /*
    ret = AT_MonitorAll(fd,buf);
    printf("[AT_MonitorAll]----->[%d]:%s\n",ret,buf);

    ret = AT_Set_MonitorRev(fd,buf,0x00);
    printf("[AT_Set_MonitorReceiver]----->[%d]:%s\n",ret,buf);

    ret = AT_Set_MonitorTran(fd,buf,0x00);
    printf("[AT_Set_MonitorTransmitter]----->[%d]:%s\n",ret,buf);
    */

    ret = AT_NormalLength(fd,buf);
    printf("[AT_NormalLength]----->[%d]:%s\n",ret,buf);

    ret = AT_ProtocolClose(fd,buf);
    printf("[AT_ProtocolClose]----->[%d]:%s\n",ret,buf);

    ret = AT_Set_Responses(fd,buf,0x01);
    printf("[AT_Responses]----->[%d]:%s\n",ret,buf);

    ret = AT_Set_RevAddress(fd,buf,0x00);
    printf("[AT_Set_ReceiveAddress]----->[%d]:%s\n",ret,buf);

    ret = AT_Set_Space(fd,buf,0x01);
    printf("[AT_Set_Space]----->[%d]:%s\n",ret,buf);

    ret = AT_Set_HeaderValue_A(fd,buf,0x000);
    printf("[AT_SetHeaderVal_A]----->[%d]:%s\n",ret,buf);

    ret = AT_Set_HeaderValue_B(fd,buf,0x00,0X00,0X00);
    printf("[AT_SetHeaderVal_B]----->[%d]:%s\n",ret,buf);

    ret = AT_Set_Protocol(fd,buf,0x04);
    printf("[AT_Set_Protocol]----->[%d]:%s\n",ret,buf);

    ret = AT_Set_ProtocolAuto(fd,buf,0x00);
    printf("[AT_Set_ProtocolAuto]----->[%d]:%s\n",ret,buf);

    ret = AT_Set_Timeout(fd,buf,0x00);
    printf("[AT_Set_Timeout]----->[%d]:%s\n",ret,buf);

    ret = AT_Try_Protocol(fd,buf,0x00);
    printf("[AT_Set_Protocol]----->[%d]:%s\n",ret,buf);

    ret = AT_Try_ProtocolAuto(fd,buf,0x00);
    printf("[AT_Set_ProtocolAuto]----->[%d]:%s\n",ret,buf);


    /*<----- J1850 Specific Commands ----->*/
    ret = AT_Set_IFR(fd,buf,0x01);
    printf("[AT_Set_IFR]----->[%d]:%s\n",ret,buf);

    ret = AT_Set_IFR_Source(fd,buf,'H');
    printf("[AT_Set_IFR_Source]----->[%d]:%s\n",ret,buf);


    /*<----- ISO Specific Commands ----->*/
    ret = AT_ISO_Set_BDR_10(fd,buf);
    printf("[AT_Set_ISO_BDR_10]----->[%d]:%s\n",ret,buf);

    ret = AT_ISO_Set_BDR_96(fd,buf);
    printf("[AT_Set_ISO_BDR_96]----->[%d]:%s\n",ret,buf);

    ret = AT_ISO_Set_InitAddr(fd,buf,0x00);
    printf("[AT_Set_ISO_InitAddr]----->[%d]:%s\n",ret,buf);

    ret = AT_ISO_Get_Keyword(fd,buf);
    printf("[AT_Get_ISO_Keyword]----->[%d]:%s\n",ret,buf);

    ret = AT_ISO_Set_Keyword(fd,buf,0x01);
    printf("[AT_Set_ISO_Keyword]----->[%d]:%s\n",ret,buf);

    ret = AT_ISO_Set_WakeupInterval(fd,buf,0x00);
    printf("[AT_Set_WakeupInterval]----->[%d]:%s\n",ret,buf);

    ret = AT_ISO_Set_WakeupMSG(fd,buf,wm,sizeof(wm));
    printf("[AT_Set_WakeupMSG]----->[%d]:%s\n",ret,buf);

    ret = AT_ISO_Set_AutoFormat(fd,buf,0x01);
    printf("[AT_Set_AutoFormat]----->[%d]:%s\n",ret,buf);

    ret = AT_ISO_Set_Filter_A(fd,buf,0x000);
    printf("[AT_Set_Filter_A]----->[%d]:%s\n",ret,buf);

    ret = AT_ISO_Set_Filter_B(fd,buf,0x00,0X00,0X00,0X00);
    printf("[AT_Set_Filter_B]----->[%d]:%s\n",ret,buf);

    ret = AT_ISO_Set_FlowControl(fd,buf,0x01);
    printf("[AT_Set_FlowControl]----->[%d]:%s\n",ret,buf);


    ret = AT_ISO_Set_Mask_A(fd,buf,0x000);
    printf("[AT_Set_Mask_A]----->[%d]:%s\n",ret,buf);

    ret = AT_ISO_Set_Mask_B(fd,buf,0x00,0X00,0X00,0X00);
    printf("[AT_Set_Mask_B]----->[%d]:%s\n",ret,buf);

    ret = AT_ISO_Set_Priority(fd,buf,0x00);
    printf("[AT_Set_CAN_Priority]----->[%d]:%s\n",ret,buf);

    ret = AT_ISO_Set_RevAddr_A(fd,buf,0x000);
    printf("[AT_Set_RevAddr_A]----->[%d]:%s\n",ret,buf);

    ret = AT_ISO_Set_RevAddr_B(fd,buf,0x00,0X00,0X00,0X00);
    printf("[AT_Set_RevAddr_B]----->[%d]:%s\n",ret,buf);

    ret = AT_ISO_Get_StatusCounts(fd,buf);
    printf("[AT_Get_CAN_StatusCounts]----->[%d]:%s\n",ret,buf);

    ret = AT_ISO_Set_DLCDisplay(fd,buf,0x00);
    printf("[AT_Set_Display 0f DLC]----->[%d]:%s\n",ret,buf);

    ret = AT_ISO_Set_FC_mode(fd,buf,0x00);
    printf("[AT_Set_FlowControl Mode]----->[%d]:%s\n",ret,buf);

    ret = AT_ISO_Set_FCHeader_A(fd,buf,0x000);
    printf("[AT_Set_FlowControl Header_A]----->[%d]:%s\n",ret,buf);

    ret = AT_ISO_Set_FCHeader_B(fd,buf,0x00,0X00,0X00,0X00);
    printf("[AT_Set_FlowControl Header_B]----->[%d]:%s\n",ret,buf);

    ret = AT_ISO_Set_FCData(fd,buf,fc_data,sizeof(fc_data));
    printf("[AT_Set_FlowControl Data]----->[%d]:%s\n",ret,buf);

    ret = AT_ISO_SendRTR(fd,buf);
    printf("[AT_Send_RTR message]----->[%d]:%s\n",ret,buf);

    ret = AT_ISO_Set_DLCUse(fd,buf,0x00);
    printf("[AT_Set_DLC Use]----->[%d]:%s\n",ret,buf);


    /*<----- J1939 CAN Specific Commands ----->*/
    ret = AT_Get_DM1(fd,buf);
    printf("[AT_Get_MD1 MSG]----->[%d]:%s\n",ret,buf);

    ret = AT_Set_ELM(fd,buf);
    printf("[AT_Set_ELM]----->[%d]:%s\n",ret,buf);

    ret = AT_Set_SAE(fd,buf);
    printf("[AT_Set_SAE]----->[%d]:%s\n",ret,buf);

    ret = AT_Set_PGNMonitor_A(fd,buf,0x00,0x00);
    printf("[AT_Set_PGNMonitor_A]----->[%d]:%s\n",ret,buf);

    ret = AT_Set_PGNMonitor_B(fd,buf,0x00,0x00,0X00);
    printf("[AT_Set_PGNMonitor_B]----->[%d]:%s\n",ret,buf);




    /********** ST Commands *********/
    /*<----- General ----->*/
    //ret = ST_Set_BRD_Software(fd,buf,38400);
    //printf("[ST_Set_BaudRate_Software]----->[%d]:%s\n",ret,buf);

    //ret = ST_Set_BRT(fd,buf,65535);
    //printf("[ST_Set_Timeout]----->[%d]:%s\n",ret,buf);

    ret = ST_Set_Description(fd,buf,"Corefaster OBD");
    printf("[ST_Set_Description]----->[%d]:%s\n",ret,buf);

    ret = ST_Set_DeviceID(fd,buf,"CF_OBD_V1.0B");
    printf("[ST_Set_DeviceID]----->[%d]:%s\n",ret,buf);

    //ret = ST_Set_BRD_Terminal(fd,buf,38400);
    //printf("[ST_Set_BaudRate_Terminal]----->[%d]:%s\n",ret,buf);

    //ret = ST_Set_BRtoNVM(fd,buf,38400);
    //printf("[ST_Set_BaudRateToNVW]----->[%d]:%s\n",ret,buf);

    /*<----- Device ID ----->*/
    ret = ST_Get_DeivceID(fd,buf);
    printf("[ST_Get_DeviceID]----->[%d]:%s\n",ret,buf);

    ret = ST_Get_FirmwareID(fd,buf);
    printf("[ST_Get_FirmwareID]----->[%d]:%s\n",ret,buf);

    ret = ST_Get_ManufactureID(fd,buf);
    printf("[ST_Get_ManufactureID]----->[%d]:%s\n",ret,buf);

    ret = ST_Get_SerialNum(fd,buf);
    printf("[ST_Get_SerialNum]----->[%d]:%s\n",ret,buf);


    /*<----- PowerSave ----->*/
    ret = ST_Get_PowerSaveConfig(fd,buf);
    printf("[ST_Get_PowerSaveConfig]----->[%d]\n",ret);
    printf("%s\n",buf);

    /*
    ret = ST_EnterSleep(fd,buf,200);
    printf("[ST_EnterSleep]----->[%d]:%s\n",ret,buf);
    */

    ret = ST_Get_LastSWTrigger(fd,buf);
    printf("[ST_Get_Last_Sleep/Wake_Trigger]----->[%d]:%s\n",ret,buf);

    ret = ST_Set_Output_Polarity(fd,buf,0);
    printf("[ST_Set_Last_OutputPolarity]----->[%d]:%s\n",ret,buf);

    ret = ST_Set_UartSW_Trigger(fd,buf,0,1);
    printf("[ST_Set_Uart_Sleep/Wake Trigger]----->[%d]:%s\n",ret,buf);

    ret = ST_Set_UartInactivityTimeout(fd,buf,1200);
    printf("[ST_Set_Uart_Inactivity Timeout]----->[%d]:%s\n",ret,buf);

    ret = ST_Set_UartWakeTiming(fd,buf,0,3000);
    printf("[ST_Set_Uart_Wakeup Timing]----->[%d]:%s\n",ret,buf);

    ret = ST_Set_ExternalSW_Trigger(fd,buf,0x00,0x01);
    printf("[ST_Set_External_Sleep/Wake Trigger]----->[%d]:%s\n",ret,buf);

    ret = ST_Set_ExternalPolarity(fd,buf,0);
    printf("[ST_Set_External_Polarity]----->[%d]:%s\n",ret,buf);

    ret = ST_Get_ExternalStatus(fd,buf);
    printf("[ST_Set_External_Status]----->[%d]:%s\n",ret,buf);

    ret = ST_Set_MinActiveTime(fd,buf,3000);
    printf("[ST_Set_MinActiveTime]----->[%d]:%s\n",ret,buf);

    ret = ST_Set_MinInactiveTime(fd,buf,2000);
    printf("[ST_Set_MinInactiveTime]----->[%d]:%s\n",ret,buf);



    /*<----- ISO Specific ----->*/
    ret = ST_ISO_Set_MaxTiming(fd,buf,1);
    printf("[ST_Set_MaxTiming On/Off]----->[%d]:%s\n",ret,buf);

    //ret = ST_ISO_Set_BR(fd,buf,38400);
    //printf("[ST_Set_Baud rate]----->[%d]:%s\n",ret,buf);

    ret = ST_ISO_Set_ManChecksum(fd,buf,0);
    printf("[ST_Set_Manual Checksum]----->[%d]:%s\n",ret,buf);

    ret = ST_ISO_Set_MaxtimeRev(fd,buf,20);
    printf("[ST_Set_Max Time for Reveive]----->[%d]:%s\n",ret,buf);

    ret = ST_ISO_Set_TimeTransmit(fd,buf,4);
    printf("[ST_Set_Time for Transmitting]----->[%d]:%s\n",ret,buf);


    /*<----- CAN Specific ----->*/
    ret = ST_Set_FlowControl(fd,buf,0x7e0,0x7e8);
    printf("[ST_Set_FlowControl]----->[%d]:%s\n",ret,buf);

    ret = ST_ClearFlowControl(fd,buf);
    printf("[ST_Clear FlowControl]----->[%d]:%s\n",ret,buf);


    /*<----- Monitoring ----->*/
    /*
    ret = ST_Monitor(fd,buf);
    printf("[ST_Monitor_OBD_BUS]----->[%d]:%s\n",ret,buf);

    ret = ST_MonitorAll(fd,buf);
    printf("[ST_Monitor_All]----->[%d]:%s\n",ret,buf);
    */


    /*<----- Filtering ----->*/

    ret = ST_AddPassFilter(fd,buf,0X7E8,0X7FF);
    printf("[ST_AddPassFilters]----->[%d]:%s\n",ret,buf);

    ret = ST_AddBlockFilter(fd,buf,0X7E8,0X7FF);
    printf("[ST_AddBlockFilters]----->[%d]:%s\n",ret,buf);

    ret = ST_AddFCFilter(fd,buf,0X7E8,0X7FF);
    printf("[ST_AddFlowControlFilters]----->[%d]:%s\n",ret,buf);


    ret = ST_ClearPassFilters(fd,buf);
    printf("[ST_ClearPassFilters]----->[%d]:%s\n",ret,buf);

    ret = ST_ClearBlockFilters(fd,buf);
    printf("[ST_ClearBlockFilters]----->[%d]:%s\n",ret,buf);

    ret = ST_ClearFCFilters(fd,buf);
    printf("[ST_ClearFCFilters]----->[%d]:%s\n",ret,buf);
}

